<template>
  <div style="width: 100%;height: 100vh;overflow: hidden;">
    <div style="width: 300px;margin:150px auto;">
      <el-card class="box-card" shadow="always" style="width: 100%">
        <template #header>
          <div class="card-header">
            <span>欢迎登录</span>
          </div>
        </template>
        <el-form ref="form" :model="form" size="default">
          <el-form-item>
            <el-input prefix-icon="UserFilled" v-model="form.username" placeholder="用户名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-input prefix-icon="Lock" v-model="form.password" placeholder="密码" show-password clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
          </el-form-item>
          <div style="text-align: right;color: cyan;font-size: 16px;text-decoration:underline;cursor:pointer" @click="toRegister">
            <span>注册</span>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      form: {},
    }
  },
  methods: {
    login() {
      request.post("/api/user/login",this.form).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "登录成功"
          })
          sessionStorage.setItem("user",JSON.stringify(res.data))
          this.$router.push("/");
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    },
    toRegister(){
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}
</style>
